/* @(#)GlyphCanvas.java
** 
** Copyright (C) 2012 by Steven D. Stamps
**
**             Trademarks & Copyrights
** Flywheel Management Science(TM), Flywheel Management Model(TM),
** Flywheel Story Editor(TM) and FlywheelMS(TM) are exclusive trademarks
** of Steven D. Stamps and may only be used freely for the purpose of
** identifying the unforked version of this software.  Subsequent forks
** may not use these trademarks.  All other rights are reserved.
** and FlywheelMS(TM) are exclusive trademarks of Steven D. Stamps
** and may only be used freely for the purpose of identifying the
** unforked version of this software.  Subsequent forks (if any) may
** not use these trademarks.  All other rights are reserved.
**
** DecKanGL (Decorated Kanban Glyph Language) and TribKn (Tribal Knowledge)
** are also exclusive trademarks of Steven D. Stamps.  These may be used
** freely within the unforked FlywheelMS application and documentation.
** All other rights are reserved.
**
** gConGUI (game Controller Graphical User Interface) is an exclusive
** trademark of Steven D. Stamps.  This trademark may be used freely
** within the unforked FlywheelMS application and documentation.
** All other rights are reserved.
**
** Trademark information is available at
** <http://www.flywheelms.com/trademarks>
**
** Flywheel Management Science(TM) is a copyrighted body of management
** metaphors, governance processes, and leadership techniques that is
** owned by Steven D. Stamps.  These copyrighted materials may be freely
** used, without alteration, by the community (users and developers)
** surrounding this GPL3-licensed software.  Additional copyright
** information is available at <http://www.flywheelms.org/copyrights>
**
**              GPL3 Software License
** This program is free software: you can use it, redistribute it and/or
** modify it under the terms of the GNU General Public License, version 3,
** as published by the Free Software Foundation. This program is distributed
** in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
** even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
** PURPOSE.  See the GNU General Public License for more details. You should
** have received a copy of the GNU General Public License, in a file named
** COPYING, along with this program.  If you cannot find your copy, see
** <http://www.gnu.org/licenses/gpl-3.0.html>.
*/

package com.flywheelms.library.deckangl.glyph;

import java.util.HashMap;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import com.flywheelms.library.deckangl.DecKanGlHelper;
import com.flywheelms.library.deckangl.enumerator.DecoratorCanvasLocation;
import com.flywheelms.library.deckangl.interfaces.DecKanGlDecorator;

public class GlyphCanvas {
	
	private transient Bitmap nounStateBitmap;
	private int nounStateBitmapResourceId;
	private HashMap<DecoratorCanvasLocation, DecKanGlDecorator> decoratorMap;
	private DecKanGlDictionary decKanGlGlyphDictionary;

	public GlyphCanvas(
			int aNounStateBitmapResourceId,
			HashMap<DecoratorCanvasLocation, DecKanGlDecorator> aDecoratorMap,
			DecKanGlDictionary aDecKanGlGlyphDictionary ) {
		this.nounStateBitmapResourceId = aNounStateBitmapResourceId;
		this.decoratorMap = aDecoratorMap;
		this.decKanGlGlyphDictionary = aDecKanGlGlyphDictionary;
	}
	
	public int getNounStateBitmapResourceId() {
		return this.nounStateBitmapResourceId;
	}

	private Bitmap getNounStateBitmap() {
		BitmapFactory.Options theBitmapOptions = new BitmapFactory.Options();
		theBitmapOptions.inScaled = false;
		if(this.nounStateBitmap == null) {
			this.nounStateBitmap = BitmapFactory.decodeResource(
					DecKanGlHelper.getResources(),
					this.nounStateBitmapResourceId,
					theBitmapOptions);
		}
		return this.nounStateBitmap;
	}
	
	public HashMap<DecoratorCanvasLocation, DecKanGlDecorator> getDecoratorMap() {
		return this.decoratorMap;
	}

	public int getMarginLeft() {
		return this.decKanGlGlyphDictionary.getMarginCanvasLeft() + 1; // TODO - HACK ALERT !!!  SDS
	}

	public int getMarginRight() {
		return this.decKanGlGlyphDictionary.getMarginCanvasRight() + 1; // TODO - HACK ALERT !!!  SDS
	}

	public int getMarginTop() {
		return this.decKanGlGlyphDictionary.getMarginCanvasTop() + 1; // TODO - HACK ALERT !!!  SDS
	}

	public int getMarginBottom() {
		return this.decKanGlGlyphDictionary.getMarginCanvasBottom() + 1; // TODO - HACK ALERT !!!  SDS
	}
	
	public int getWidthLeftZone() {
		if (!DecKanGlDictionary.decoratorsUseZone(
				this.decoratorMap, CanvasZone.LEFT_ZONE)) {
			return 0;
		}
		return this.decKanGlGlyphDictionary.getWidthLeftZone();
	}
	
	public int getWidthRightZone() {
		if (!DecKanGlDictionary.decoratorsUseZone(
				this.decoratorMap, CanvasZone.RIGHT_ZONE)) {
			return 0;
		}
		return this.decKanGlGlyphDictionary.getWidthRightZone();
	}
	
	public int getMarginNounStatusLeft() {
		return this.decKanGlGlyphDictionary.getMarginNounStatusLeft();
	}
	
	public int getMarginNounStatusRight() {
		return this.decKanGlGlyphDictionary.getMarginNounStatusRight();
	}
	
	public int getMarginNounStatusTopMargin() {
		return this.decKanGlGlyphDictionary.getMarginNounStatusTop();
	}
	
	public int getMarginNounStatusBottomMargin() {
		return this.decKanGlGlyphDictionary.getMarginNounStatusBottom();
	}
	
	public int getWidthNounStatusBitmap() {
		return getNounStateBitmap().getWidth();
	}
	
	public int getHeightNounStatusBitmap() {
		return getNounStateBitmap().getHeight();
	}
	
	public int getHeightTopZone() {
		if (!DecKanGlDictionary.decoratorsUseZone(
				this.decoratorMap, CanvasZone.TOP_ZONE)) {
			return 0;
		}
		return this.decKanGlGlyphDictionary.getHeightTopZone();
	}
	
	public int getHeightBottomZone() {
		if (!DecKanGlDictionary.decoratorsUseZone(
				this.decoratorMap, CanvasZone.BOTTOM_ZONE)) {
			return 0;
		}
		return this.decKanGlGlyphDictionary.getHeightBottomZone();
	}

	public int getWidth() {
		return 	getMarginLeft() +
				getWidthLeftZone() +
				getCenterWidth() +
				getWidthRightZone() +
				getMarginRight();
	}
	
	public int getCenterWidth() {
		return Math.max(getMaxWidthTopBottomZones(), getTotalNounStatusWidth());
	}
	
	private int getTotalNounStatusWidth() {
		return getWidthNounStatusBitmap() + getMarginNounStatusLeft() + getMarginNounStatusRight();
	}
	
	private int getMaxWidthTopBottomZones() {
		if (!DecKanGlDictionary.decoratorsUseZone(this.decoratorMap, CanvasZone.TOP_ZONE) &&
				!DecKanGlDictionary.decoratorsUseZone(this.decoratorMap, CanvasZone.BOTTOM_ZONE)) {
			return 0;
		}
		if (DecKanGlDictionary.decoratorsUseZone(this.decoratorMap, CanvasZone.TOP_ZONE) &&
				!DecKanGlDictionary.decoratorsUseZone(this.decoratorMap, CanvasZone.BOTTOM_ZONE)) {
			return this.decKanGlGlyphDictionary.getWidthTopZone();
		}
		if (!DecKanGlDictionary.decoratorsUseZone(this.decoratorMap, CanvasZone.TOP_ZONE) &&
				DecKanGlDictionary.decoratorsUseZone(this.decoratorMap, CanvasZone.BOTTOM_ZONE)) {
			return this.decKanGlGlyphDictionary.getWidthBottomZone();
		}
		return Math.max(this.decKanGlGlyphDictionary.getWidthTopZone(), this.decKanGlGlyphDictionary.getWidthBottomZone());
	}
	
	private int getMaxHeightLeftRightZones() {
		if (!DecKanGlDictionary.decoratorsUseZone(this.decoratorMap, CanvasZone.LEFT_ZONE) &&
				!DecKanGlDictionary.decoratorsUseZone(this.decoratorMap, CanvasZone.RIGHT_ZONE)) {
			return 0;
		}
		if (DecKanGlDictionary.decoratorsUseZone(this.decoratorMap, CanvasZone.LEFT_ZONE) &&
				!DecKanGlDictionary.decoratorsUseZone(this.decoratorMap, CanvasZone.RIGHT_ZONE)) {
			return this.decKanGlGlyphDictionary.getHeightLeftZone();
		}
		if (!DecKanGlDictionary.decoratorsUseZone(this.decoratorMap, CanvasZone.LEFT_ZONE) &&
				DecKanGlDictionary.decoratorsUseZone(this.decoratorMap, CanvasZone.RIGHT_ZONE)) {
			return this.decKanGlGlyphDictionary.getHeightRightZone();
		}
		return Math.max(this.decKanGlGlyphDictionary.getHeightLeftZone(), this.decKanGlGlyphDictionary.getHeightRightZone());
	}

	public int getHeight() {
		return 	getMarginTop() +
				getHeightTopZone() +
				getCenterHeight() +
				getHeightBottomZone() +
				getMarginBottom();
	}
	
	public int getCenterHeight() {
		return Math.max(getMaxHeightLeftRightZones(), getTotalNounStatusHeight());
	}
	
	private int getTotalNounStatusHeight() {
		return getHeightNounStatusBitmap() + getMarginNounStatusTopMargin() + getMarginNounStatusBottomMargin();
	}

	int getNounStatusImageCanvasPositionX() {
		return 	getMarginLeft() +
				getWidthLeftZone() +
				getMarginNounStatusLeft();
	}

	int getNounStatusImageCanvasPositionY() {
		return 	getMarginTop() +
				getHeightTopZone() +
				getMarginNounStatusTopMargin();
	}

	public int getNounBackgroundColorResourceId() {
		return this.decKanGlGlyphDictionary.getNounBackgroundColor();
	}

	public int getDecoratorBackgroundColor() {
		return this.decKanGlGlyphDictionary.getDecoratorBackgroundColor();
	}

	public int getDecoratorBackgroundColorMediumAlpha() {
		return this.decKanGlGlyphDictionary.getDecoratorBackgroundColorMediumAlpha();
	}

	public int getDecoratorBackgroundColorLightAlpha() {
		return this.decKanGlGlyphDictionary.getDecoratorBackgroundColorLightAlpha();
	}


}